home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / slip / cslip-2.6 / tip / value.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-30  |  6.0 KB  |  338 lines

  1. /*
  2.  * Copyright (c) 1983 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #ifndef lint
  21. static char sccsid[] = "@(#)value.c    5.3 (Berkeley) 6/1/90";
  22. #endif /* not lint */
  23.  
  24. #include "tip.h"
  25.  
  26. #define MIDDLE    35
  27.  
  28. static value_t *vlookup();
  29. static int vaccess();
  30. static int vtoken();
  31. static int vprint();
  32. static int col = 0;
  33.  
  34. /*
  35.  * Variable manipulation
  36.  */
  37. vinit()
  38. {
  39.     register value_t *p;
  40.     register char *cp;
  41.     FILE *f;
  42.     char file[256];
  43.  
  44.     for (p = vtable; p->v_name != NULL; p++) {
  45.         if (p->v_type&ENVIRON)
  46.             if (cp = getenv(p->v_name))
  47.                 p->v_value = cp;
  48.         if (p->v_type&IREMOTE)
  49.             number(p->v_value) = *address(p->v_value);
  50.     }
  51.     /*
  52.      * Read the .tiprc file in the HOME directory
  53.      *  for sets
  54.      */
  55.     strcpy(file, value(HOME));
  56.     strcat(file, "/.tiprc");
  57.     if ((f = fopen(file, "r")) != NULL) {
  58.         register char *tp;
  59.  
  60.         while (fgets(file, sizeof(file)-1, f) != NULL) {
  61.             if (vflag)
  62.                 printf("set %s", file);
  63.             if (tp = rindex(file, '\n'))
  64.                 *tp = '\0';
  65.             vlex(file);
  66.         }
  67.         fclose(f);
  68.     }
  69.     /*
  70.      * To allow definition of exception prior to fork
  71.      */
  72.     vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
  73. }
  74.  
  75. /*VARARGS1*/
  76. vassign(p, v)
  77.     register value_t *p;
  78.     char *v;
  79. {
  80.  
  81.     if (!vaccess(p->v_access, WRITE)) {
  82.         printf("access denied\r\n");
  83.         return;
  84.     }
  85.     switch (p->v_type&TMASK) {
  86.  
  87.     case STRING:
  88.         if (equal(p->v_value, v))
  89.             return;
  90.         if (!(p->v_type&(ENVIRON|INIT)))
  91.             free(p->v_value);
  92.         if ((p->v_value = malloc(size(v)+1)) == NOSTR) {
  93.             printf("out of core\r\n");
  94.             return;
  95.         }
  96.         p->v_type &= ~(ENVIRON|INIT);
  97.         strcpy(p->v_value, v);
  98.         break;
  99.  
  100.     case NUMBER:
  101.         if (number(p->v_value) == number(v))
  102.             return;
  103.         number(p->v_value) = number(v);
  104.         break;
  105.  
  106.     case BOOL:
  107.         if (boolean(p->v_value) == (*v != '!'))
  108.             return;
  109.         boolean(p->v_value) = (*v != '!');
  110.         break;
  111.  
  112.     case CHAR:
  113.         if (character(p->v_value) == *v)
  114.             return;
  115.         character(p->v_value) = *v;
  116.     }
  117.     p->v_access |= CHANGED;
  118. }
  119.  
  120. vlex(s)
  121.     register char *s;
  122. {
  123.     register value_t *p;
  124.  
  125.     if (equal(s, "all")) {
  126.         for (p = vtable; p->v_name; p++)
  127.             if (vaccess(p->v_access, READ))
  128.                 vprint(p);
  129.     } else {
  130.         register char *cp;
  131.  
  132.         do {
  133.             if (cp = vinterp(s, ' '))
  134.                 cp++;
  135.             vtoken(s);
  136.             s = cp;
  137.         } while (s);
  138.     }
  139.     if (col > 0) {
  140.         printf("\r\n");
  141.         col = 0;
  142.     }
  143. }
  144.  
  145. static int
  146. vtoken(s)
  147.     register char *s;
  148. {
  149.     register value_t *p;
  150.     register char *cp;
  151.     char *expand();
  152.  
  153.     if (cp = index(s, '=')) {
  154.         *cp = '\0';
  155.         if (p = vlookup(s)) {
  156.             cp++;
  157.             if (p->v_type&NUMBER)
  158.                 vassign(p, atoi(cp));
  159.             else {
  160.                 if (strcmp(s, "record") == 0)
  161.                     cp = expand(cp);
  162.                 vassign(p, cp);
  163.             }
  164.             return;
  165.         }
  166.     } else if (cp = index(s, '?')) {
  167.         *cp = '\0';
  168.         if ((p = vlookup(s)) && vaccess(p->v_access, READ)) {
  169.             vprint(p);
  170.             return;
  171.         }
  172.     } else {
  173.         if (*s != '!')
  174.             p = vlookup(s);
  175.         else
  176.             p = vlookup(s+1);
  177.         if (p != NOVAL) {
  178.             vassign(p, s);
  179.             return;
  180.         }
  181.     }
  182.     printf("%s: unknown variable\r\n", s);
  183. }
  184.  
  185. static int
  186. vprint(p)
  187.     register value_t *p;
  188. {
  189.     register char *cp;
  190.     extern char *interp(), *ctrl();
  191.  
  192.     if (col > 0 && col < MIDDLE)
  193.         while (col++ < MIDDLE)
  194.             putchar(' ');
  195.     col += size(p->v_name);
  196.     switch (p->v_type&TMASK) {
  197.  
  198.     case BOOL:
  199.         if (boolean(p->v_value) == FALSE) {
  200.             col++;
  201.             putchar('!');
  202.         }
  203.         printf("%s", p->v_name);
  204.         break;
  205.  
  206.     case STRING:
  207.         printf("%s=", p->v_name);
  208.         col++;
  209.         if (p->v_value) {
  210.             cp = interp(p->v_value, NULL);
  211.             col += size(cp);
  212.             printf("%s", cp);
  213.         }
  214.         break;
  215.  
  216.     case NUMBER:
  217.         col += 6;
  218.         printf("%s=%-5d", p->v_name, number(p->v_value));
  219.         break;
  220.  
  221.     case CHAR:
  222.         printf("%s=", p->v_name);
  223.         col++;
  224.         if (p->v_value) {
  225.             cp = ctrl(character(p->v_value));
  226.             col += size(cp);
  227.             printf("%s", cp);
  228.         }
  229.         break;
  230.     }
  231.     if (col >= MIDDLE) {
  232.         col = 0;
  233.         printf("\r\n");
  234.         return;
  235.     }
  236. }
  237.  
  238.  
  239. static int
  240. vaccess(mode, rw)
  241.     register unsigned mode, rw;
  242. {
  243.     if (mode & (rw<<PUBLIC))
  244.         return (1);
  245.     if (mode & (rw<<PRIVATE))
  246.         return (1);
  247.     return ((mode & (rw<<ROOT)) && getuid() == 0);
  248. }
  249.  
  250. static value_t *
  251. vlookup(s)
  252.     register char *s;
  253. {
  254.     register value_t *p;
  255.  
  256.     for (p = vtable; p->v_name; p++)
  257.         if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
  258.             return (p);
  259.     return (NULL);
  260. }
  261.  
  262. char *
  263. vinterp(s, stop)
  264.     register char *s;
  265.     char stop;
  266. {
  267.     register char *p = s, c;
  268.     int num;
  269.  
  270.     while ((c = *s++) && c != stop)
  271.         switch (c) {
  272.  
  273.         case '^':
  274.             if (*s)
  275.                 *p++ = *s++ - 0100;
  276.             else
  277.                 *p++ = c;
  278.             break;
  279.  
  280.         case '\\':
  281.             num = 0;
  282.             c = *s++;
  283.             if (c >= '0' && c <= '7')
  284.                 num = (num<<3)+(c-'0');
  285.             else {
  286.                 register char *q = "n\nr\rt\tb\bf\f";
  287.  
  288.                 for (; *q; q++)
  289.                     if (c == *q++) {
  290.                         *p++ = *q;
  291.                         goto cont;
  292.                     }
  293.                 *p++ = c;
  294.             cont:
  295.                 break;
  296.             }
  297.             if ((c = *s++) >= '0' && c <= '7') {
  298.                 num = (num<<3)+(c-'0');
  299.                 if ((c = *s++) >= '0' && c <= '7')
  300.                     num = (num<<3)+(c-'0');
  301.                 else
  302.                     s--;
  303.             } else
  304.                 s--;
  305.             *p++ = num;
  306.             break;
  307.  
  308.         default:
  309.             *p++ = c;
  310.         }
  311.     *p = '\0';
  312.     return (c == stop ? s-1 : NULL);
  313. }
  314.  
  315. /*
  316.  * assign variable s with value v (for NUMBER or STRING or CHAR types)
  317.  */
  318.  
  319. vstring(s,v)
  320.     register char *s;
  321.     register char *v;
  322. {
  323.     register value_t *p;
  324.     char *expand();
  325.  
  326.     p = vlookup(s); 
  327.     if (p == 0)
  328.         return (1);
  329.     if (p->v_type&NUMBER)
  330.         vassign(p, atoi(v));
  331.     else {
  332.         if (strcmp(s, "record") == 0)
  333.             v = expand(v);
  334.         vassign(p, v);
  335.     }
  336.     return (0);
  337. }
  338.